package com.cloud.zhimao.system.user.web.rest;


import com.cloud.zhimao.common.api.QQInterConnection;
import com.cloud.zhimao.common.base.web.rest.IBaseRest;
import com.cloud.zhimao.common.config.annotation.ApiLog;
import com.cloud.zhimao.common.config.annotation.Authority;
import com.cloud.zhimao.common.config.annotation.VisitController;
import com.cloud.zhimao.common.config.exception.CloudException;
import com.cloud.zhimao.common.config.result.Result;
import com.cloud.zhimao.system.user.pojo.entity.User;
import com.cloud.zhimao.system.user.service.UserService;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author Long
 */
@Authority(root = true)
@Slf4j
@CrossOrigin
@RestController
//@Controller
//@RequestMapping("/user/rest")
@RequestMapping("/")
@Api(tags = "用户管理")
public class UserRest extends IBaseRest<User, Long, UserService, UserRest> {
    @Autowired
    private QQInterConnection qqInterConnection;


    @ApiLog(value = "QQ授权登录", model = "用户管理")
    @VisitController
    @ApiOperation(value = "QQ授权登录")
    @GetMapping(value = "loginByQQ")
    public void loginByQQ(HttpServletRequest req, HttpServletResponse res){
        try {
            res.setContentType("text/html;charset=utf-8");
            String authorizeURL = new Oauth().getAuthorizeURL(req);
            res.sendRedirect(authorizeURL);
        } catch (QQConnectException | IOException e) {
            throw new CloudException("QQ授权登录异常");
        }
    }

    @ApiLog(value = "QQ授权登录回调", model = "用户管理")
    @VisitController
    @ApiOperation(value = "QQ授权登录回调")
    @GetMapping(value = "home")
    public Result<?> loginQQBack(HttpServletRequest req, HttpServletResponse res){
        return Result.ok(service.loginByQQ(req, res));
    }

    @ApiLog(value = "QQ授权登录回调", model = "用户管理")
    @VisitController
    @ApiOperation(value = "QQ授权登录回调")
    @GetMapping(value = "home2")
    public ModelAndView qqb(HttpServletRequest req, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("/index");
//        UserInfoBean userInfoBean = qqInterConnection.getUserInfo(req);
//        modelAndView.addObject("name", userInfoBean.getNickname());
//        modelAndView.addObject("avatar", userInfoBean.getAvatar().getAvatarURL30());
        /*try {



            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            String accessToken   = null,
                    openID        = null;
            long tokenExpireIn = 0L;
            if (accessTokenObj.getAccessToken().equals("")) {
                System.out.print("没有获取到响应参数");
            }else{
                accessToken = accessTokenObj.getAccessToken();
                tokenExpireIn = accessTokenObj.getExpireIn();
                OpenID openIDObj =  new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();

                modelAndView.addObject("name", userInfoBean.getNickname());
                modelAndView.addObject("avatar", userInfoBean.getAvatar().getAvatarURL30());

            }
        }catch(Exception e){
            e.printStackTrace();
        }*/

        return modelAndView;
    }
}
